﻿@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout_lay_admin.cshtml";
}

<div class="layuimini-container">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset">
            <legend>@ViewBag.TitleSearch</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form" action="#">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">@ViewBag.User_UserName</label>
                            <div class="layui-input-inline">
                                <input type="text" id="username" name="username" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">@ViewBag.Status</label>
                            <div class="layui-input-inline">
                                <select name="available" id="available" lay-search="">
                                    <option value="">全部</option>
                                    <option value="N">禁用</option>
                                    <option value="Y">正常</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">@ViewBag.SearchKey</label>
                            <div class="layui-input-inline">
                                <input type="text" name="keyword" id="keyword" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button id="btn-search" class="layui-btn " lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> @ViewBag.Search</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>
        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button ex_btn="ex_add" class="layui-btn layui-btn-sm data-add-btn" lay-event="add"> @ViewBag.Add </button>
                <button ex_btn="ex_freezing" class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="freezing"> @ViewBag.Freezing </button>
                <button ex_btn="ex_thawing" class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="thawing"> @ViewBag.Thawing </button>
                <button ex_btn="ex_export" class="layui-btn layui-btn-sm " lay-event="export"> @ViewBag.Export </button>
            </div>
        </script>
        <table class="layui-hide formeldboard" id="currentTableId" lay-filter="currentTableFilter"></table>
        <script type="text/html" id="currentTableBar">
            <a ex_btn="ex_edit" class="layui-btn layui-btn-xs data-count-edit" lay-event="edit"> @ViewBag.Edit</a>
            @*<a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">禁用</a>*@
        </script>
    </div>
</div>
<script src="/Content/layuimini/lib/jquery-3.4.1/jquery-3.4.1.min.js"></script>
<script>
    var table;

    var reloadTable = function (sortField, sortType) {
        var keyword = $("#keyword").val();
        var username = $("#username").val();
        var available = $("#available").val();
        table.reload('currentTableId', {
            where: { //设定异步数据接口的额外参数，任意设
                keyword: keyword,
                username: username,
                available: available,
                sortField: sortField,
                sortType: sortType
            }
        });
    }

    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form;

        window.table = layui.table;

        // 监听搜索操作
        //form.on('submit(data-search-btn)', function (data) {
        //    var result = JSON.stringify(data.field);
        //    layer.alert(result, {
        //        title: '最终的搜索信息'
        //    });
        //    //执行搜索重载
        //    table.reload('currentTableId', {
        //        page: {
        //            curr: 1
        //        }
        //        , where: {
        //            searchParams: result
        //        }
        //    }, 'data');
        //    return false;
        //});
        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '添加用户',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['800px', '680px'],
                    content: '/BaseUser/Create',
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'freezing') {  // 监听删除操作
                var checkStatus = table.checkStatus('currentTableId');
                var data = checkStatus.data;

                layer.confirm('确定要禁用选中的账户吗？', function () {
                    var arrId = [];
                    for (var i = 0; i < data.length; i++) {
                        arrId.push(data[i].User_Id);
                    }
                    var strIds = arrId.join(',');

                    $.ajax({
                        url: '/BaseUser/ActivateSwich',
                        type: 'post',
                        dataType: 'text',
                        data: { "Ids": strIds },
                        success: function (resp) {
                            var json = eval('(' + resp + ')');
                            layer.alert(json.Message);
                            reloadTable();
                        },
                        error: function (resp) {
                            layer.alert("对不起出错了");
                        }
                    })
                })

                //layer.alert(JSON.stringify(data));
            }else if (obj.event === 'thawing') {  // 监听删除操作
                var checkStatus = table.checkStatus('currentTableId');
                var data = checkStatus.data;

                layer.confirm('确定要解冻选中的账户吗？', function () {
                    var arrId = [];
                    for (var i = 0; i < data.length; i++) {
                        arrId.push(data[i].User_Id);
                    }
                    var strIds = arrId.join(',');

                    $.ajax({
                        url: '/BaseUser/ActivateSwich',
                        type: 'post',
                        dataType: 'text',
                        data: { "Ids": strIds,activate:'Y' },
                        success: function (resp) {
                            var json = eval('(' + resp + ')');
                            layer.alert(json.Message);
                            reloadTable();
                        },
                        error: function (resp) {
                            layer.alert("对不起出错了");
                        }
                    })
                })

                //layer.alert(JSON.stringify(data));
            } else if (obj.event === 'export') {
                var keyword = $("#keyword").val();
                var username = $("#username").val();
                var available = $("#available").val();
                var url = "/BaseUser/Export?keyword=" + keyword + "&username=" + username + "&available=" + available;
                window.location.href = url;

            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            //console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {

                var index = layer.open({
                    title: '编辑用户',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['800px', '600px'],
                    content: '/BaseUser/Edit?primarykey=' + data.User_Id,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            }
        });

        table.on('sort(currentTableFilter)', function (obj) {
            reloadTable(obj.field, obj.type);
        });

        $('#btn-search').click(function () {
            reloadTable();
            return false;
        });

        var _url = '/BaseUser/Search';
        table.render({
            elem: '#currentTableId',
            url: _url,
            where: { sortField: "User_UserName",
                sortType: "desc"},
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'print'],
            cols: [[
                { type: "checkbox", width: 50 },
                //{ field: 'id', width: 80, title: 'ID', sort: true },
                { field: 'User_UserName', width: 200, title: '账户', sort: true },
                { field: 'User_RealName', width: 150, title: '姓名', sort: true },
                { field: 'User_Email', width: 200, title: '邮件', sort: true },
                { field: 'User_Sex', width: 80, title: '性别', sort: true },
                { field: 'User_PhoneNumber', width: 160, title: '手机', sort: true },
                { field: 'User_CompanyName', width: 160, title: '单位名称' },
                { field: 'User_DepartmentName', width: 120, title: '部门' },                
                { field: 'Organization_Name', width: 130, title: '职位' },
                { field: 'UserRoleRemark', width: 100, title: '角色' },
                { field: 'CreateUserName', width: 100, title: '创建人', sort: true },
                {
                    field: 'User_Available', width: 80, title: '状态', sort: true, templet: function (d) {
                        if (d.User_Available == 'Y') {
                            return "正常";
                        }
                        if (d.User_Available == 'N') {
                            return "禁用"
                        }
                        return d.User_Available;
                    }
                },
                {
                    field: 'CreateDateTime', width: 120, title: '创建时间', templet: function (d) {
                        return layuidatetimeFormatter(d.CreateDateTime, "yyyy-MM-dd");
                    }
                },
                { title: '操作', minWidth: 120, toolbar: '#currentTableBar', align: "center", fixed: 'right'}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 10,
            page: true
            //skin: 'line'
        });

    });

    function aaa() {
        reloadTable();
    }
</script>
